package com.aliyun.tools.importdialog;

import java.io.FileNotFoundException;

import org.gnome.gdk.Event;
import org.gnome.glade.Glade;
import org.gnome.glade.XML;
import org.gnome.gtk.AboutDialog;
import org.gnome.gtk.ButtonsType;
import org.gnome.gtk.ComboBox;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.MessageDialog;
import org.gnome.gtk.MessageType;
import org.gnome.gtk.Notebook;
import org.gnome.gtk.ResponseType;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

public class ToolMainWindow {
	/**
	 * 将从glade文件中获得窗口信息，保存到字段中，进行相关操作，来改变窗口显示情况。
	 * @author jian.hj
	 */
	static XML mainWin;
	
	static Window winMain;
	static VBox vboxBottom;
	static Fixed fixedTop;
	static Fixed fixedCenterSearch;
	static Fixed fixedBottomSearch;
	static Fixed fixedImport;
	static Toolbar toolBar;
	static ToolButton toolBtnSearch;
	static ToolButton toolBtnImport;
	static ToolButton toolBtnExport;
	static ToolButton toolBtnAbout;
	static ToolButton toolBtnQuit;
	
	static ComboBox comboBox;
	
	static Notebook noteBook;
	/**
	 * 
	 */
	public int width;
	public int height;
	/**
	 * 导入窗口填写信息对话框
	 * @author jian.hj
	 */
	static ImportDialog importDialog;
	private boolean importDialogPressed = false;
//	public static void main(String[] args){
//		
//		Gtk.init(args);  
//		try{
//			new ToolMainWindow();
//		}catch(Exception e){
//			//doing nothing
//		}
//		Gtk.main();
//	}
	public ToolMainWindow()throws FileNotFoundException{
		/**
		 *  从glade文件中读取窗口配置信息。
		 *  @author jian.hj
		 */
		mainWin = Glade.parse("resources/tool_winMain.glade", "main_window");    
		
		winMain = (Window) mainWin.getWidget("main_window");
		vboxBottom = (VBox) mainWin.getWidget("vbox_bottom");
		fixedTop = (Fixed) mainWin.getWidget("fixed_top");
		fixedCenterSearch = (Fixed)mainWin.getWidget("fixed_center_search");
		fixedBottomSearch = (Fixed)mainWin.getWidget("fixed_bottom_search");
		fixedImport = (Fixed)mainWin.getWidget("fixed_import");
		toolBtnSearch = (ToolButton) mainWin.getWidget("toolbtn_search");
		toolBtnImport = (ToolButton) mainWin.getWidget("toolbtn_import");
		toolBtnExport = (ToolButton) mainWin.getWidget("toolbtn_export");
		toolBtnAbout = (ToolButton) mainWin.getWidget("toolbtn_about");
		toolBtnQuit = (ToolButton) mainWin.getWidget("toolbtn_quit");
		comboBox = (ComboBox) mainWin.getWidget("combox_category");
		noteBook = (Notebook) mainWin.getWidget("notebook_import");
		importDialog = new ImportDialog(winMain, width, height);
		
		this.width = winMain.getWidth();
		this.height = winMain.getHeight();
		winMain.connect(
			new Window.DeleteEvent() {
			public boolean onDeleteEvent(Widget arg0, Event arg1) {
				Gtk.mainQuit();  
				return false;
			}
		});
		
		/**
		 * 搜索按钮
		 * @author jian.hj
		 */
		toolBtnSearch.connect(
				new ToolButton.Clicked() {
					public void onClicked(ToolButton arg0) {
						// TODO Auto-generated method stub
						setSearchShowAll();
						setNoteBookHide();
						importDialogPressed = false;
						if(importDialog.getImportDialogShowed() == true)
							importDialog.hide();
						//TODO： need some data operation , search data from database with version or name ...
					}
				});
		
		/**
		 * 导入按钮
		 * @author jian.hj
		 */
		toolBtnImport.connect(
				new ToolButton.Clicked() {
					public void onClicked(ToolButton arg0) {
						// TODO Auto-generated method stub
						setNoteBookShowAll();
						setSearchHide();
						importDialogPressed = true;
						if(importDialog.getImportDialogShowed() == false)
							importDialog.showAll();			
					}
				});
		
		/**
		 *导出按钮
		 *@author jian.hj
		 */
		toolBtnExport.connect(
				new ToolButton.Clicked() {
					public void onClicked(ToolButton arg0) {
						// TODO Auto-generated method stub
						if(importDialogPressed == true){
							setNoteBookShowAll();
							setSearchHide();
						}else{
							setSearchShowAll();
							setNoteBookHide();
						}
					}
				});
		
		/**
		 * 关于按钮
		 * @author jian.hj
		 */
		toolBtnAbout.connect(
				new ToolButton.Clicked() {
					public void onClicked(ToolButton arg0) {
						// TODO Auto-generated method stub
						AboutDialog aboutDialog = new AboutDialog();
						aboutDialog.setProgramName("XMLCompareTool");
						aboutDialog.setComments("测试结果xml文件对比工具，可查询到单个测试类相关信息，以及导入xml文件时产生同以往信息的对比结果，同时，提供对比结果导出。");
						aboutDialog.setCopyright("aliyun.com, 2011, made by jian.hj and ruanyi.ry ");
						aboutDialog.getChildren();
						ResponseType choice = aboutDialog.run();
						if(choice == ResponseType.CLOSE || choice == ResponseType.CANCEL || choice == ResponseType.NO || choice == ResponseType.NONE){
							aboutDialog.hide();
						}
					}
				});
		
		/**
		 * 退出按钮
		 * @author jian.hj
		 */
		toolBtnQuit.connect(
				new ToolButton.Clicked() {
					public void onClicked(ToolButton arg0) {
						// TODO Auto-generated method stub
						MessageDialog messageDialog = new MessageDialog(winMain, true, MessageType.WARNING, ButtonsType.YES_NO, "确定退出？");
						ResponseType choice = messageDialog.run();
						if(choice == ResponseType.YES){
							Gtk.mainQuit(); 
						}else{
							messageDialog.hide();
						}
					}
				});
		
		windowDefault(vboxBottom);
	}
	
	/**
	 * 初始化窗口，动态增加状态栏
	 * @param vboxBottom
	 * @author jian.hj
	 */
	public void windowDefault(VBox vboxBottom){
		setVersionChooser();
	}
	/**
	 * 获得版本号信息
	 * @author jian.hj
	 */
	public void setVersionChooser(){
		comboBox.setActive(0);
		
		//TODO :need data operation, for example ,get version data or others.
	}
	
	public void setNoteBookHide(){
		fixedImport.hide();
	}
	
	public void setSearchHide(){
		fixedCenterSearch.hide();
		fixedBottomSearch.hide();
	}
	
	public void setNoteBookShowAll(){
		fixedImport.showAll();
	}
	
	public void setSearchShowAll(){
		fixedCenterSearch.showAll();
		fixedBottomSearch.showAll();
	}
	
	public void showAll(){
		winMain.showAll();
	}
	public void hide(){
		winMain.hide();
	}

	public void getVersionFromdb(){
		
	}
}
